
//  AddContactViewController.m
//  test2
//
//  Created by 贺银银 on 16/3/25.
//  Copyright © 2016年 hyy. All rights reserved.
//

#import "AddContactViewController.h"
#import "CDChatManager.h"
#import "ChatRoomViewController.h"
#import "UserFactory.h"
#import "SVProgressHUD.h"
#import "ChatRoomViewController.h"
#import "AVStatus.h"
@interface AddContactViewController ()//<CDUserDelegate,CDUserModelDelegate>

@end

@implementation AddContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.userArray = [NSMutableArray array];
//    NSLog(@"%@",self.userArray);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)addContactAction:(UIButton *)sender {
    
    if(self.userClientIDTextField.text.length > 0)
    {
        
//        AVQuery *query1 = [AVUser followeeQuery:@"USER_OBJECT_ID"];
//        [query1 includeKey:@"followee"];
        
        
        
        AVQuery *query = [AVQuery queryWithClassName:@"_User"];
        [query whereKey:@"username" equalTo:self.userClientIDTextField.text];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (objects.count > 0) {
                
                AVUser *user1 = objects[0];
                [[AVUser currentUser] follow:user1.objectId andCallback:^(BOOL succeeded, NSError *error) {
                    
                    if (succeeded) {
                        
                        if ([self.userArray containsObject:user1.username]) {
                            [SVProgressHUD showErrorWithStatus:@"该用户已是您好友,请回好友列表查看"];
                        }
                        else{
                            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"成功找到好友" message:@"聊天请Go Chat,返回请Cancle" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler:nil];
                            UIAlertAction *goChat = [UIAlertAction actionWithTitle:@"Go Chat" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                
                                [[CDChatManager manager] fetchConversationWithOtherId:self.userClientIDTextField.text callback:^(AVIMConversation *conversation, NSError *error) {
                                    
                                    ChatRoomViewController *chatRoomVC = [[ChatRoomViewController alloc]initWithConversation:conversation];
                                    
                                    [self.navigationController pushViewController:chatRoomVC animated:YES];
                                }];
                            }];
                            [alert addAction:goChat];
                            [alert addAction:cancle];
                            [self presentViewController:alert animated:YES completion:nil];
                        }
                    }
                
                }];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:@"查无此好友"];
            }
        }];
        
    }

}

@end
